package cn.bob.pet.controller;

import cn.bob.basic.jwt.LoginContext;
import cn.bob.org.domain.Employee;
import cn.bob.pet.dto.HandleDto;
import cn.bob.org.mapper.EmployeeMapper;
import cn.bob.pet.service.ISearchMasterMsgService;
import cn.bob.pet.domain.SearchMasterMsg;
import cn.bob.pet.query.SearchMasterMsgQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.user.domain.Logininfo;
import cn.bob.user.domain.User;
import cn.bob.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IUserService userService;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delById(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     *寻主消息保存
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userService.findLoginid(logininfo.getId());
        if (user!=null) {
            searchMasterMsg.setUserId(user.getId());
        }
        return  searchMasterMsgService.publish(searchMasterMsg);

    }

    /**
     * 待处理数据
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogid(logininfo.getId());
        //审核通过
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(employee.getShopId());
        return  searchMasterMsgService.toHandle(searchMasterMsg);

    }
    /**
     * 待审核数据
     * 只有平台管理员能看
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsg){
        //待审核
        searchMasterMsg.setState(0);
        return  searchMasterMsgService.toAudit(searchMasterMsg);

    }   /**
     * 待审核数据
     * 只有平台管理员能看  为1 且为null
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsg){
        //待审核
        searchMasterMsg.setState(1);
        return  searchMasterMsgService.pool(searchMasterMsg);

    }

    /**
     * 已完成数据
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogid(logininfo.getId());
        //审核通过
        searchMasterMsg.setState(3);
        searchMasterMsg.setShopId(employee.getShopId());
        return  searchMasterMsgService.finish(searchMasterMsg);

    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandleDto handleDto,HttpServletRequest request) throws UnsupportedEncodingException {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogid(logininfo.getId());

        searchMasterMsgService.accept(handleDto,employee);
        return AjaxResult.me().success();
    }

    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogid(logininfo.getId());

        searchMasterMsgService.reject(msgId,employee);
        return AjaxResult.me().success();
    }
}
